package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.FeeKey;
import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.service.FeeKeyService;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.vo.fee.FeeKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Tag(name = "房间杂费管理")
@RestController
@RequestMapping("/admin/fee")
@Slf4j
public class FeeController {

    @Autowired
    private FeeKeyService feeKeyService;

    @Autowired
    private FeeValueService feeValueService;

    @Operation(summary = "保存或更新杂费名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateFeeKey(@RequestBody FeeKey feeKey) {
        feeKeyService.saveOrUpdate(feeKey);
        return Result.ok();
    }

    @Operation(summary = "保存或更新杂费值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateFeeValue(@RequestBody FeeValue feeValue) {
        feeValueService.saveOrUpdate(feeValue);
        return Result.ok();
    }


    @Operation(summary = "查询全部杂费名称和杂费值列表")
    @GetMapping("list")
    public Result<List<FeeKeyVo>> feeInfoList() {

        long startTime = System.currentTimeMillis();

        // 使用Stream进行封装数据
        List<FeeKey> keyList = feeKeyService.list();
        Map<Long, List<FeeValue>> valueListMap = feeValueService.list().stream()
                .collect(Collectors.groupingBy(FeeValue::getFeeKeyId));

        List<FeeKeyVo> list = keyList.stream().map(feeKey -> {
            FeeKeyVo feeKeyVo = new FeeKeyVo();
            BeanUtils.copyProperties(feeKey, feeKeyVo);
            feeKeyVo.setFeeValueList(valueListMap.get(feeKey.getId()));
            return feeKeyVo;
        }).collect(Collectors.toList());

        long endTime = System.currentTimeMillis();
        log.info("costTime:"+(endTime - startTime) + "ms");

       //  循环方法,不推荐使用,数据量大速度会很慢
       /* long startTime = System.currentTimeMillis();
        List<FeeKeyVo> list = feeKeyService.list().stream().map(feeKey -> {
            FeeKeyVo feeKeyVo = new FeeKeyVo();
            BeanUtils.copyProperties(feeKey, feeKeyVo);
            feeKeyVo.setFeeValueList(feeValueService.list(new LambdaQueryWrapper<FeeValue>().eq(FeeValue::getFeeKeyId, feeKeyVo.getId())));
            return feeKeyVo;
        }).collect(Collectors.toList());
        long endTime = System.currentTimeMillis();
        log.info("costTime:"+(endTime - startTime) + "ms");*/
        return Result.ok(list);
    }

    @Operation(summary = "根据id删除杂费名称")
    @DeleteMapping("key/deleteById")
    @Transactional
    public Result deleteFeeKeyById(@RequestParam Long feeKeyId) {
        feeKeyService.removeById(feeKeyId);
        feeValueService.remove(new LambdaQueryWrapper<FeeValue>().eq(FeeValue::getFeeKeyId,feeKeyId));
        return Result.ok();
    }

    @Operation(summary = "根据id删除杂费值")
    @DeleteMapping("value/deleteById")
    public Result deleteFeeValueById(@RequestParam Long id) {
        feeValueService.removeById(id);
        return Result.ok();
    }
}
